#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<arpa/inet.h>

#define BUFFER_NUM 1024
uint8_t buff_file[BUFFER_NUM];

typedef struct LED_Cmd
{
    uint8_t id;
    uint8_t brightness;
    uint8_t hue;
    uint8_t expand;
}LED_Cmd;

int main()
{
    // 创建网络连接
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd == -1)
    {
        perror("Get fd fail\n");
        return -1;
    }
    struct sockaddr_in server_addr;
    server_addr.sin_family = AF_INET;     //协议
    server_addr.sin_port = htons(7675);   //设定端口号，并进行大小端序的转换
    inet_pton(AF_INET, "192.168.2.178", &server_addr.sin_addr.s_addr);
    printf("%lx\n", server_addr.sin_addr.s_addr);
    int ret = connect(fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    if(ret == -1)
    {
        perror("connect error");
        return -1;
    }

    int times = 0;
    char buff[1024];
    FILE* fp = NULL;  
    // 打开文件
    // 图像
    // fp = fopen("/home/cui/catkin_ws/src/anyka_ctrl/demo/gesture/handle/zan.yuv, "rb"); 

    // 语音
    fp = fopen("/mnt/Audio_Capture/AudioStream.pcm", "rb"); 

    int len;  
    // 读取和传输数据
    while(!feof(fp)){
        len = (unsigned int)fread(buff_file, 1, BUFFER_NUM, fp);  
        send(fd, buff_file, len, 0);
        sleep(0.001);
    }
    LED_Cmd cmd;
    while(1)
    {
        len = recv(fd, buff, sizeof(buff), 0);
        if(len > 0)     //len为正数表示接收到len个数据
        {
            memcpy(&cmd, buff, 4);
            // 语音指令
            printf("id: %d, bright: %d, hue: %d\n", cmd.id, cmd.brightness, cmd.hue);  

            // 手势指令
            // printf("cmd: %s\n", buff);  
            // printf("cmd: %d\n", buff[0] - '0');  
        }
        sleep(1);
    }
    
    close(fd);

    return 0;
}   